<?php
/**
 * Created by PhpStorm.
 * User: qishengqiang
 * Date: 2020-12-02
 * Time: 00:23
 */

namespace app\services;


use app\models\ModelBase;

class UtilService
{
    public function  uuid()
    {
        $chars = md5(uniqid(mt_rand(), true));
        $uuid = substr ( $chars, 0, 8 ) . '-'
            . substr ( $chars, 8, 4 ) . '-'
            . substr ( $chars, 12, 4 ) . '-'
            . substr ( $chars, 16, 4 ) . '-'
            . substr ( $chars, 20, 12 );
        return $uuid ;
    }

    public function getMeta($model)
    {
//         return [
//             'first'       => 1,
//             'before'      => $model->currentPage() > 1 ?$model->currentPage() - 1:$model->currentPage(),
//             'current'     => $model->currentPage(),
//             'last'        => $model->lastPage(),
//             'next'        => $model->currentPage() < $model->lastPage()?$model->currentPage() + 1:$model->lastPage(),
//             'total_pages' => $model->lastPage(),
//             'total_items' => $model->total(),
//             'limit'       => $model->listRows(),
//         ];
        return [
            'current'   =>  $model->currentPage(),
            'pages'     =>  $model->lastPage(),
            'size'      =>  $model->listRows(),
            'total'     =>  $model->total()
        ];
    }

    /**
     * 生成支付商户号
     * @return string
     */
    public function getOrderNo($userId)
    {
        // 时间戳 -- 8位
        $date = date('Ymd');

        // 用户标示 -- 6位
        $user = str_pad($userId, 6, '0', STR_PAD_LEFT);

        // 毫秒 -- 4位
        list($usec, $sec) = explode(" ", microtime());
        $usec = str_pad(substr($usec, 2, 4), 4, '0', STR_PAD_RIGHT);

        // 随机数 -- 2位
        $rand = str_pad(rand(0, 99), 2, '0', STR_PAD_LEFT);

        // 合并输出
        return $date . $user . $usec . $rand;
    }

    public function getAlphaSort($sort)
    {
        $alpha = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];

        return $alpha[$sort];
    }

    /**
     * 获取本周所有日期
     */
    public function getWeek($time = '', $format = 'Y-m-d'){
        $time = $time != '' ? $time : time();
        //获取当前周几
        $week = date('w', $time);
        $date = [];
        for ($i=1; $i<=7; $i++){
            $date[$i] = date($format ,strtotime( '+' . ($i-$week) .' days', $time));
        }
        return $date;
    }
}